<?php
namespace Admin\Controller;

use Admin\Controller\GlobalController;

class StatisticsmashController extends GlobalController
{
    public function getlist()
    {
        if (I('get.day')) {
            $this->day();
            exit();
        }

        $prModel = M('smash_record');
        $start_time = I('get.start_time', strtotime(date('Y-m-d')) - 86400 * 30, 'strtotime');
        $end_time = I('get.end_time', 0, 'strtotime');
        $end_time = $end_time ? $end_time + 86399 : time();
        $where['add_time'] = array(array('egt', $start_time), array('elt', $end_time));
        $this->assign('start_time', date('Y-m-d', $start_time));
        $this->assign('end_time', date('Y-m-d', $end_time));
        $id = I('get.id', '', 'trim');
        $this->assign('id', $id);

        $labelData = array();
        if ($id) {
            $arr = explode(',', $id);
            foreach ($arr as $intId) {
                $where['egg_id'] = $intId;
                $data = $prModel->field("COUNT(record_id) as num, FROM_UNIXTIME(add_time, '%Y%m%d') as ymd, add_time")
                    ->where($where)
                    ->order('ymd desc')
                    ->group('ymd')
                    ->select();
                $labelData[$intId] = array("id={$intId}", fillBlankDate($data, $start_time, $end_time));
            }

        } else {
            $data = $prModel->field("COUNT(record_id) as num, FROM_UNIXTIME(add_time, '%Y%m%d') as ymd, add_time")
                ->where($where)
                ->order('ymd desc')
                ->group('ymd')
                ->select();
            $labelData = array(array('all', fillBlankDate($data, $start_time, $end_time)));
        }

        $this->assign('labelData', $labelData);
        $this->display();
    }

    public function day() {
        $day = I('get.day', date('Ymd'), 'trim');
        $start_time = strtotime($day);
        $end_time = $start_time + 86399;
        $prModel = M('smash_record');
        $where['add_time'] = array(array('egt', $start_time), array('elt', $end_time));
        $id = I('get.id', '', 'trim');
        $this->assign('id', $id);
        if ($id) {
            $where['egg_id'] = $id;
        }

        //当天
        $labelData = array();
        $data = $prModel->field("COUNT(*) as num, FROM_UNIXTIME(add_time, '%H') as day_h, add_time")
            ->where($where)
            ->order('day_h desc')
            ->group('day_h')
            ->select();
        $labelData[] = array($day, fillBlankHour($data, $start_time, $end_time));

        //前一天
        $prev_start = $start_time - 86400;
        $prev_end = $prev_start + 86399;
        $where['add_time'] = array(array('egt', $prev_start), array('elt', $prev_end));
        $data = $prModel->field("COUNT(*) as num, FROM_UNIXTIME(add_time, '%H') as day_h, add_time")
            ->where($where)
            ->order('day_h desc')
            ->group('day_h')
            ->select();
        $labelData[] = array(date('Ymd', $prev_start), fillBlankHour($data, $prev_start, $prev_end));

        $this->assign('day', $day);
        $this->assign('labelData', $labelData);
        $this->ajaxReturn($this->fetch('day'), 'success', 'TIP_SUCCESS');
    }
}